<?php

namespace core;

class View
{
    private $cachePath = null;
    private $options = [
        //网站标题
        'title' => '',
        //网站简介
        'description' => '',
        //网站关键词
        'keywords' => '',
        //网站作者
        'author' => 'kyo <iprintf@qq.com>',
        //自定义meta数据
        'meta' => [],
        //网站资源目录
        'root' => PUBLIC_PATH,
        //css资源路径
        'css' => [
            ['path' => 'css/bootstrap.min.css', 'abs' => false],
        ],
        //js资源路径
        'js' => [
            ['path' => 'js/jquery.min.js', 'abs' => false],
            ['path' => 'js/bootstrap.min.js', 'abs' => false],
        ],
        //head标签的扩展接口
        'head_pad' => '',
        'body' => '',
        //body头部扩展接口
        'body_top_pad' => '',
        //body尾部扩展接口
        'body_bottom_pad' => '',
    ];

    public function checkCache()
    {
        $cache_file = C('MODULE_NAME').DS.C('CONTROLLER_NAME').DS;
        $cache_file .= C('ACTION_NAME').DS.C('ACTION_ARG');
        $this->cachePath = RUN_PATH.'cache'.DS.sha1($cache_file).C('TEMPLATE_SUFFIX');
        if (file_exists($this->cachePath)) {
            $time = time() - filemtime($this->cachePath);
            if ($time < C('CACHE_TIME'))
                kyo::halt(readfile($this->cachePath));
        }
    }

    public function __construct(array $options = [])
    {
        if (C('CACHE'))
            $this->checkCache();
        $this->options = $options + $this->options;
    }

    public function setOption($name, $val = null)
    {
        if (is_array($name))
            $this->options = $name + $this->options;
        else
            $this->options[$name] = $val;
    }

    public function getOption($name = null)
    {
        return $name ? $this->options[$name] : $this->options;
    }

    public function addRes($type, $file, $abs = false)
    {
        $this->options[$type][] = ['path' => $file, $abs = $abs];
    }

    public function delRes($type, $index)
    {
        unset($this->options[$type][$index]);
    }

    public function getBody()
    {

    }

    static public function getMeta($meta)
    {
        $html = "";
        foreach ($meta as $v) {
            $html .= '<meta '.$v.'>';
        }

        return $html;
    }

    static public function getCss($css, $root = null)
    {
        $html = "";
        foreach ($css as $v) {
            if ($v['abs']) {
                $html .= '<link href="'.$v['path'].'" rel="stylesheet">';
            } else {
                $html .= '<link href="'.$root.$v['path'].'" rel="stylesheet">';
            }
        }

        return $html;
    }

    static public function getJS($js, $root = null)
    {
        $html = "";
        foreach ($js as $v) {
            if ($v['abs']) {
                $html .= '<script src="'.$v['path'].'"></script>';
            } else {
                $html .= '<script src="'.$root.$v['path'].'"></script>';
            }
        }

        return $html;
    }

    public function display($return = true)
    {
        ob_start();
        extract($this->options);
        $css = self::getCss($css, $root);
        $js = self::getJs($js, $root);
        $meta = self::getMeta($meta);
        // $body = $this->getBody();

        $html = <<< EOF
<!DOCTYPE html>
<html lang="zh-cn">
  <head>
  <meta charset="utf-8">
  <meta name="description" content="$description">
  <meta name="keywords" content="$keywords">
  <meta name="author" content="$author">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  $meta
  <title>$title</title>
  $css
  $head_pad
</head>
<body>
  $body_top_pad
  $body
  $body_bottom_pad
  $js
  </body>
</html>
EOF;
        echo $html;

        // echo dump($this->cachePath);

        $content = ob_get_contents();

        if ($this->cachePath != null)
            file_put_contents($this->cachePath, $content);

        if ($return)
            return $content;

        ob_end_flush();
    }
}
